import { Request, Response } from 'express';
import { random } from 'lodash';


// 模拟角色数据
const users: any[] = [
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
    {
        id: 0,
        name: '张三',
        mobile: '13269751369',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        role: ['管理员',' 研发'],
        department: '技术部',
        status: 0,
        createAt: '2025-03-15 12:34:56',
    },
];

// 模拟分页数据
const getPageData = (params: any) => {
    const { current = 1, pageSize = 10 } = params;
    const start = (current - 1) * pageSize;
    const end = start + Number(pageSize);
    return {
        content: users.slice(start, end),
        totalElements: users.length,
        current: Number(current),
        pageSize: Number(pageSize),
    };
};

export default {
    'POST /api/system/user/login': async (req: Request, res: Response) => {
        const { password, username, type } = req.body;
        await waitTime(2000);
        if (password === '123456' && username === 'admin') {
            res.send({
                code: 0,
                msg: '登录成功',
                data: {
                    accessToken: random(32),
                    name: 'admin',
                    avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
                    role: ['admin'],
                    department: '技术部',
                    nickname: 'GM',
                },
            });
            return;
        }
        res.send({
            code: 400,
            msg: '用户名或密码错误',
        });
    },
    'GET /api/system/user/current': async (req: Request, res: Response) => {
        await waitTime(10);
        const token = req.header('Authorization')?.replace('Bearer ', '');
        if (!token) {
            res.send({
                code: 400,
                msg: 'token无效',
            });
            return;
        }
        res.send({
            code: 0,
            msg: 'SUCCESS',
            data: {
                accessToken: token,
                name: 'admin',
                avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
                role: ['admin'],
                department: '技术部',
                nickname: 'GM',
            },
        });
    },
    'GET /api/system/user': async (req: Request, res: Response) => {
        const pageData = getPageData(req.query);
        res.status(200).json({
            code: 200,
            message: '获取用户列表成功',
            data: pageData,
        });
    },
};

const waitTime = (time: number = 100) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(true);
        }, time);
    });
};
